import Observe from './Observe.js'
import Compile from './Compile.js'

export default class Vue {
  constructor(options) {
    this.$el = options.el
    this.$data = options.data || {}
    this.$methods = options.methods || {}
    this.options = options
    //执行数据拦截
    new Observe(this.$data)
    //执行代理拦截
    this.proxyDdata(this.$data)
    //编译
    new Compile(this, this.$el)
  }
  //添加一个转换代理方法proxyDdata，目的为了让vm.name 等同于vm.$data.name,拦截Vue实例的属性访问
  proxyDdata(data) {
    for (const [key, value] of Object.entries(data)) {
      //拦截this 就是拦截实例
      Object.defineProperty(this, key, {
        get() {
          return data[key]
        },
        set(newValue) {
          data[key] = newValue
        },
      })
    }
  }
}
